layui.use(['form','admin', 'ax', 'table', 'treetable'], function () {
    var $ax = layui.ax;
    var admin = layui.admin;
    var table = layui.table;
    var treetable = layui.treetable;
    var form = layui.form;

    var Current = {
        tableId: "table"
    };

    Current.initColumn = function () {
        return [[
            {type: 'numbers'},
            {field: 'id', hide: true, sort: true, title: 'id'},
            {field: 'appId', hide: true, sort: true, title: '应用id'},
            {field: 'name', sort: true, title: '应用名称'},
            {field: 'description', sort: true, title: '描述'},
            {align: 'center', toolbar: '#tableBar', title: '操作', minWidth: 200}
        ]];
    };

    Current.search = function () {
        var queryData = {
            name: $('#name').val()
        };
        table.reload(Current.tableId, {where:queryData});
    };

    Current.initTable = function (tableId, data) {
        return treetable.render({
            elem: '#' + tableId,
            url: system.contextPath + '/admin/sysApplication/search/self',
            where: data,
            page: false,
            height: "full-158",
            cellMinWidth: 100,
            cols: Current.initColumn(),
            treeColIndex: 2,
            treeSpid: "0",
            treeIdName: "id",
            treePidName: "pid",
            treeDefaultClose: true,
            treeLinkage: false
            ,isJump: true //是否允许点击节点时弹出新窗口跳转
            ,showLine: true  //是否开启连接线
            ,size: 'lg',
            done: function (res, curr, count) {
                count || this.elem.next('.layui-table-view').find('.layui-table-header').css('overflow', 'visible');
            $('.layui-table-body').find('div.layui-table-cell').find('select').each(function () {
                var $this = $(this);
                var val = $this.data('value');
                var text;
                $(this).children().each(function () {
                    if (val == this.value) {
                        text = this.text;
                        return;
                    }
                })
                //更新layui 选择器输入框的值
                $this.next().find('.layui-select-title').find('input').val(text);
                //更新layui 选择器的值 以及设置其样式
                $this.val(val).parents('div.layui-table-cell').css('overflow', 'visible');
            })
            form.render(null, 'table');
            }
        });
    };

    Current.onUse = function (data) {
        system.useSelectorData(data);
    };

    // 渲染表格
    var tableResult = Current.initTable(Current.tableId);

    $('#expandAll').click(function () {
        treetable.expandAll('#' + Current.tableId);
    });
    $('#foldAll').click(function () {
        treetable.foldAll('#' + Current.tableId);
    });

    // 工具条点击事件
    table.on('tool(' + Current.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if(layEvent === 'use'){
            Current.onUse(data);
        }
    });
});
